import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import ServicesBase from "../Base/ServicesBase";
import {
  IAppleSnakeMap,
  IConfigMap,
  IGameModeImgMap,
  IGuideMap,
  ILevelInfo,
  INormalLevelStruct,
  ISkinMap,
  ISourceMap,
} from "../Interface";

export default class ConfigService extends ServicesBase {
  serviceName: string = "===配置表服务";

  private m_sourceList: IConfigMap[] = null; // 所有配置表的列表
  private m_newestList: IConfigMap[] = null; // 最新关卡模式配置表的列表
  private m_shenqiList: IConfigMap[] = null; // 神奇汉字模式配置表的列表
  private m_wangengList: IConfigMap[] = null; // 玩梗消除模式配置表的列表
  private m_lianxianList: IConfigMap[] = null; // 连线达人模式配置表的列表

  private m_normalLevelList: ILevelInfo[] = [];
  public get NormalLevelList() {
    return this.m_normalLevelList;
  }
  private m_newestLevelList: ILevelInfo[] = [];
  public get NewestLevelList() {
    return this.m_newestLevelList;
  }
  private m_shenqiLevelList: ILevelInfo[] = [];
  public get ShenqiLevelList() {
    return this.m_shenqiLevelList;
  }
  private m_wangengLevelList: ILevelInfo[] = [];
  public get WanGengLevelList() {
    return this.m_wangengLevelList;
  }
  private m_lianxianLevelList: ILevelInfo[] = [];
  public get LianXianLevelList() {
    return this.m_lianxianLevelList;
  }

  private m_levelSoureList: ISourceMap[] = null; // 各模式关卡的配置表列表,用以给关卡系统初始化
  public get LevelSourceList() {
    return this.m_levelSoureList;
  }
  private m_newestMSLevelList: ISourceMap[] = null; // 最新关卡模式关卡的配置表列表,用以给关卡系统初始化
  public get NewestMSLevelSourceList() {
    return this.m_newestMSLevelList;
  }
  private m_shenqiMSLevelList: ISourceMap[] = null; // 神奇汉字模式关卡的配置表列表,用以给关卡系统初始化
  public get ShenqiMSLevelSourceList() {
    return this.m_shenqiMSLevelList;
  }
  private m_wangengMSLevelList: ISourceMap[] = null; // 玩梗消除模式关卡的配置表列表,用以给关卡系统初始化
  public get WangengMSLevelSourceList() {
    return this.m_wangengMSLevelList;
  }
  private m_lianxianMSLevelList: ISourceMap[] = null; // 连线达人模式关卡的配置表列表,用以给关卡系统初始化
  public get LianxianMSLevelSourceList() {
    return this.m_lianxianMSLevelList;
  }


  private m_normalGuideList: IGuideMap = null;
  public get NormalGuideList() {
    return this.m_normalGuideList;
  }

  //TODO 新模式请看这里

  private m_modeImgMap: IGameModeImgMap = <IGameModeImgMap>{};
  public get ModeImgMap() {
    return this.m_modeImgMap;
  }

  private m_skinMap: ISkinMap = <ISkinMap>{};
  public get SkinMap() {
    return this.m_skinMap;
  }
  public GetSkin(id) {
    return this.m_skinMap[id];
  }

  private m_appleSnakeMap: IAppleSnakeMap = <IAppleSnakeMap>{};
  public get AppleSnakeMap() {
    return this.m_appleSnakeMap;
  }
  public GetAppleSnakeLevel(id) {
    return this.m_appleSnakeMap[id];
  }

  async ServiceInit(): Promise<any> {
    super.ServiceInit();
    /** 配置默认模式 */
    this.m_sourceList = [
      { path: "Json/NormalLevelConfig", responName: "m_normalLevelList" },
    ];
    this.m_levelSoureList = [
      { mode: GAME_MODE.NORMAL, responName: "NormalLevelList" },
    ];

    let array = this.m_sourceList;
    for (let index = 0; index < array.length; index++) {
      const element = array[index];

      await this.LoadFun(element.path, element.responName);
    }
    /** 配置最新关卡 */
    this.m_newestList = [
      { path: "Json/NormalNewestLevelConfig", responName: "m_newestLevelList" }
    ]
    this.m_newestMSLevelList = [
      { mode: GAME_MODE.CARD_COMPOSITION, responName: "NewestLevelList" },
    ];
    let arrayNewest = this.m_newestList;
    for (let index = 0; index < arrayNewest.length; index++) {
      const element = arrayNewest[index];

      await this.LoadFun(element.path, element.responName);
    }
    /** 配置神奇汉字 */
    this.m_shenqiList = [
      { path: "Json/NormalShenQiLevelConfig", responName: "m_shenqiLevelList" }
    ]
    this.m_shenqiMSLevelList = [
      { mode: GAME_MODE.SHENQIHANZI, responName: "ShenqiLevelList" },
    ];
    let arrayShenQi = this.m_shenqiList;
    for (let index = 0; index < arrayShenQi.length; index++) {
      const element = arrayShenQi[index];

      await this.LoadFun(element.path, element.responName);
    }
    /** 配置玩梗消除 */
    this.m_wangengList = [
      { path: "Json/NormalWanGengLevelConfig", responName: "m_wangengLevelList" }
    ]
    this.m_wangengMSLevelList = [
      { mode: GAME_MODE.WANGENG, responName: "WanGengLevelList" },
    ];
    let arrayWanGeng = this.m_wangengList;
    for (let index = 0; index < arrayWanGeng.length; index++) {
      const element = arrayWanGeng[index];

      await this.LoadFun(element.path, element.responName);
    }
    /** 配置连线达人 */
    this.m_lianxianList = [
      { path: "Json/NormalLianxianLevelConfig", responName: "m_lianxianLevelList" }
    ]
    this.m_lianxianMSLevelList = [
      { mode: GAME_MODE.LIANXIAN, responName: "LianXianLevelList" },
    ];
    let arrayLianXian = this.m_lianxianList;
    for (let index = 0; index < arrayLianXian.length; index++) {
      const element = arrayLianXian[index];

      await this.LoadFun(element.path, element.responName);
    }
  }

  async LoadFun(path: string, responName: string) {
    await new Promise<void>((resolve, reject) => {
      cc.resources.load(path, (err, asset: cc.JsonAsset) => {
        if (err) {
          reject(err);
          console.error(`加载配置${path}失败`, err);
          return;
        }
        this[responName] = asset.json;
        //this.Log("##",responName,this[responName]);
        resolve();
      });
    });
  }
}
